package com.renwei.yunlong.service;

import android.app.IntentService;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;

import com.renwei.yunlong.event.SaveImg2GallaryEvent;
import com.renwei.yunlong.utils.ImgUtils;

import de.greenrobot.event.EventBus;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.service
 * Created by :  Benjamin.
 * Created time: 2017/11/16 19:30
 * Changed by :  Benjamin.
 * Changed time: 2017/11/16 19:30
 * Class description:
 */

public class ImageSaveService extends IntentService {
    private static String TAG ="ImageSaveService";
    public static String imagPath = "";

    public ImageSaveService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        imagPath = intent.getStringExtra("imagePath");
        Bitmap bitmap = ImgUtils.getBitMBitmap(imagPath);
        if (bitmap != null) {
            Boolean isSave = ImgUtils.saveImageToGallery(getApplicationContext(),bitmap);
            if (isSave) {
                //图片已经保存到相册
                EventBus.getDefault().post(new SaveImg2GallaryEvent(true));
            } else {
                //图片保存到相册的时候失败
                EventBus.getDefault().post(new SaveImg2GallaryEvent(false));
            }

        } else {
            //图片不存在
            EventBus.getDefault().post(new SaveImg2GallaryEvent(false));
        }
    }
}
